/**
 * Copyright (c) 2019 xstgongyi.org
 * All rights reserved.
 */
package org.xstgongyi.eduaid.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.xstgongyi.eduaid.common.ControllerHelper;
import org.xstgongyi.eduaid.dataobject.SponsorDO;
import org.xstgongyi.eduaid.service.SponsorService;

/**
 * @author oldcrane<br/>
 * @created 2019-04-17
 */
@Controller
@RequestMapping("/sponsors")
public class SponsorController {

	@Autowired
	private SponsorService sponsorService;

	@GetMapping("/entry")
	public String entryPage(ModelMap map) {
		return "aid/sponsor_list";
	}

	@GetMapping("")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(name = "keyword", required = false) String keyword,
			@RequestParam(name = "page", defaultValue = "1") int pageNumber,
			@RequestParam(name = "limit", defaultValue = "20") int pageSize) {
		Map<String, Object> result = new HashMap<>();
		List<SponsorDO> rows = new ArrayList<>(); // 如果没有记录，返回空数组，而非null
		keyword = null == keyword ? "" : keyword.trim();
		int total = sponsorService.countTotal(keyword);
		if (total > 0) {
			rows = sponsorService.listPage(keyword, pageNumber, pageSize);
		}
		result.put("page", pageNumber);
		result.put("total", total);
		result.put("rows", rows);
		return ControllerHelper.successResponse(result);
	}

	@GetMapping("/list_for_selection")
	@ResponseBody
	public Map<String, Object> list_for_selection(@RequestParam(name = "page", defaultValue = "1") int pageNumber,
			@RequestParam(name = "limit", defaultValue = "9999") int pageSize) {

		List<SponsorDO> sponsorList = sponsorService.listPage(null, pageNumber, pageSize);

		List<Map<String, String>> result = new ArrayList<>();
		for (SponsorDO sponsor : sponsorList) {
			Map<String, String> row = new HashMap<String, String>();
			row.put("id", String.valueOf(sponsor.getId()));
			if (null != sponsor.getNickname() && sponsor.getNickname().length() > 0) {
				row.put("name", sponsor.getName() + "(" + sponsor.getNickname() + ")");
			} else {
				row.put("name", sponsor.getName());
			}
			result.add(row);
		}
		return ControllerHelper.successResponse(result);
	}

	@PostMapping("")
	@ResponseBody
	public Map<String, Object> addSponsor(@RequestBody SponsorDO sponsor) {
		sponsorService.insert(sponsor);
		return ControllerHelper.successResponse(null);
	}

	@GetMapping("/{id}")
	@ResponseBody
	public Map<String, Object> getSponsor(@PathVariable("id") Integer id) {
		SponsorDO sponsor = sponsorService.getById(id);
		if (null == sponsor) {
			return ControllerHelper.errorResponse(404, "sponsor(id=" + id + ") not exist.");
		} else {
			return ControllerHelper.successResponse(sponsor);
		}
	}

	@PostMapping("/{id}")
	@ResponseBody
	public Map<String, Object> updateSponsor(@PathVariable("id") Integer id, @RequestBody SponsorDO inputObj) {
		SponsorDO existSponsor = sponsorService.getById(id);
		SponsorDO sponsor = SponsorControllerHelper.merge(existSponsor, inputObj);
		sponsorService.update(sponsor);
		return ControllerHelper.successResponse(null);
	}

}
